package com.wwwarehouse.common.custom_widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.wwwarehouse.common.R;

/**
 * @author : mundane
 * @time : 2017/8/14 17:15
 * @description :
 * @file : UIWarningView.java
 */

public class UIWarningRelativeLayout extends RelativeLayout {

//	public interface onStateChangedListener {
//		void onStateChane(View v, boolean isStateWarning);
//	}
//
//	private onStateChangedListener mOnStateChangedListener;
//
//	public void setOnStateChangedListener(onStateChangedListener onStateChangedListener) {
//		mOnStateChangedListener = onStateChangedListener;
//	}

	private ImageView mWarningView;

	public UIWarningRelativeLayout(Context context) {
		this(context, null);
	}

	public UIWarningRelativeLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public UIWarningRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initAttrs(context, attrs);
		initData(context);
	}

	GradientDrawable gradientDrawable;
	// 警告背景颜色
	private int warningColor = getResources().getColor(R.color.common_color_c10_eceff3);
	// 当前颜色
	private int normalColor;
	// 当前圆角
	private float currCorner = dp2px(3);
	// 描边宽度
	private float strokeWidth = 0;
	// 描边颜色
	private int strokeColor = Color.TRANSPARENT;
	// 透明颜色
	private int mTransparentColor = Color.TRANSPARENT;

	private void initData(Context context) {
		mWarningView = new ImageView(context);
		mWarningView.setVisibility(GONE);
		mWarningView.setImageResource(R.drawable.warning);
		LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
		layoutParams.rightMargin = dp2px(10);
		addView(mWarningView, layoutParams);

		gradientDrawable = new GradientDrawable();

		gradientDrawable.setColor(mTransparentColor);
//		gradientDrawable.setStroke((int) strokeWidth, strokeColor);
		gradientDrawable.setCornerRadius(currCorner);
		setBackground(gradientDrawable);
		//
	}

	public boolean isWarning() {
		if (mWarningView.getVisibility() == VISIBLE) {
			return true;
		} else {
			return false;
		}
	}

	public void setStateWarning(boolean isWarning) {
		if (isWarning) {
			gradientDrawable.setColor(warningColor);
			mWarningView.setVisibility(VISIBLE);
		} else {
			gradientDrawable.setColor(mTransparentColor);
			mWarningView.setVisibility(GONE);
		}
		requestLayout();
	}

	static class SavedState extends BaseSavedState {
		boolean isWarning;

		SavedState(Parcelable superState) {
			super(superState);
		}

		private SavedState(Parcel in) {
			super(in);
			isWarning = (Boolean) in.readValue(null);
		}

		@Override
		public void writeToParcel(Parcel out, int flags) {
			super.writeToParcel(out, flags);
			out.writeValue(isWarning);
		}

		@Override
		public String toString() {
			return "UIWarningRelativeLayout.SavedState{"
					+ Integer.toHexString(System.identityHashCode(this))
					+ " isWarning=" + isWarning + "}";
		}

		public static final Parcelable.Creator<SavedState> CREATOR
				= new Parcelable.Creator<SavedState>() {
			public SavedState createFromParcel(Parcel in) {
				return new SavedState(in);
			}

			public SavedState[] newArray(int size) {
				return new SavedState[size];
			}
		};
	}

//	@Override
//	public Parcelable onSaveInstanceState() {
//		Parcelable superState = super.onSaveInstanceState();
//
//		SavedState ss = new SavedState(superState);
//
//		ss.isWarning = mIsStateWarning;
//		return ss;
//	}
//
//	@Override
//	public void onRestoreInstanceState(Parcelable state) {
//		SavedState ss = (SavedState) state;
//
//		super.onRestoreInstanceState(ss.getSuperState());
//		boolean isWarning = ss.isWarning;
//		if (isWarning) {
//			gradientDrawable.setColor(warningColor);
//			mWarningView.setVisibility(VISIBLE);
//			mIsStateWarning = true;
//		} else {
//			gradientDrawable.setColor(mTransparentColor);
//			mWarningView.setVisibility(GONE);
//			mIsStateWarning = false;
//		}
//		requestLayout();
//	}

	public void ShowWarningView() {
		mWarningView.setVisibility(VISIBLE);
//		mIsStateWarning = true;
//		if (mOnStateChangedListener != null) {
//			mOnStateChangedListener.onStateChane(this, mIsStateWarning);
//		}
	}

	private void initAttrs(Context context, AttributeSet attrs) {
	}

	private int px2dp(float paValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (paValue / scale + 0.5f);
	}

	private int dp2px(float dpValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	private int px2sp(float pxValue) {
		final float frontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / frontScale + 0.5f);
	}

	private int sp2px(float spValue) {
		final float frontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * frontScale + 0.5f);
	}
}
